package com.ezjie.ielts.module_set;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ezjie.ielts.R;
import com.ezjie.ielts.constant.Constants;
import com.ezjie.ielts.core.base.BaseBean;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.core.http.HttpRequestAbstractCallBack;
import com.ezjie.ielts.task.FeedbackTask;
import com.ezjie.ielts.util.AppUtil;
import com.ezjie.ielts.util.LogUtils;
import com.ezjie.ielts.util.NetworkUtil;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.widget.TitleTextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

/**
 * 意见反馈
 * 
 * @author renyangyang
 *
 */
@ContentView(R.layout.layout_feedback)
public class FeedbackActivity extends BaseFragmentActivity {

	@ViewInject(R.id.iv_topbar_back)
	private ImageView iv_topbar_back;
	@ViewInject(R.id.tv_topbar_title)
	private TitleTextView tv_topbar_title;

	@ViewInject(R.id.feedback_edit_text)
	private EditText feedback_edit_text;
	@ViewInject(R.id.contact_edit_text)
	private EditText contact_edit_text;
	@ViewInject(R.id.submit_feedback_btn)
	private Button submit_feedback_btn;
	@ViewInject(R.id.tv_leave)
	private TextView tv_leave;

	private FeedbackTask feedbackTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		feedbackTask = new FeedbackTask(this);
		init();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_FEEDBACK);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_FEEDBACK);
	}

	private void init() {
		iv_topbar_back.setOnClickListener(this);
		tv_topbar_title.setText(R.string.feedback_title);
		submit_feedback_btn.setOnClickListener(this);

		feedback_edit_text.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				String string = s.toString();
				tv_leave.setText(String.format(getResources().getString(R.string.input_leave), 200 - string.length()));
			}
		});
	}

	@Override
	public void onClickEvent(View view) {
		switch (view.getId()) {
		case R.id.iv_topbar_back:
			this.finish();
			break;
		case R.id.submit_feedback_btn:
			// 友盟统计
			MobclickAgent.onEvent(this, "feedBack_commit");
			
			doSubmit();
			break;
		default:
			break;
		}
	}

	private void doSubmit() {
		String text = feedback_edit_text.getText().toString();
		String contact = contact_edit_text.getText().toString();
		if (TextUtils.isEmpty(text)) {
			AppUtil.showToast(this, R.string.please_input_content);
			return;
		}
		if (!NetworkUtil.isNetworkAvailable(this)) {
			AppUtil.showToast(this, R.string.no_network);
			return;
		}

		feedbackTask.doFeedback(text, contact, new HttpRequestAbstractCallBack(this,true) {
			@Override
			public void onStartCallBack() {
				// TODO Auto-generated method stub
				super.onStartCallBack();
				AppUtil.showProgressDialog(FeedbackActivity.this);
			}

			@Override
			public void onSuccessCallBack(String result) {
				// TODO Auto-generated method stub
				super.onSuccessCallBack(result);
				AppUtil.dismissProgressDialog();
				LogUtils.d(result);
				try {
					BaseBean baseBean = JSON.parseObject(result, BaseBean.class);
					if (baseBean != null) {
						AppUtil.showToast(FeedbackActivity.this, R.string.set_feedback_success);
						FeedbackActivity.this.finish();
					}
				} catch (Exception e) {
					LogUtils.exception(e);
				}
			}

			@Override
			public void onFailureCallBack(HttpException error, String msg) {
				// TODO Auto-generated method stub
				super.onFailureCallBack(error, msg);
				AppUtil.dismissProgressDialog();
				AppUtil.showToast(FeedbackActivity.this, R.string.load_net_data_failure);
			}
		});

	}

}
